﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace 顔写真いじったー
{
    public partial class MainForm : Form
    {
        private PhotoWrapper _adphoto;
        private const string INFOTEXT = "画像サイズ: {0}\r\nフォーマット: {1}\r\nファイルサイズ: {2:N0} バイト";
        private string _imageFilePath = "";

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _adphoto = new PhotoWrapper(Environment.UserName, Environment.UserDomainName);
            顔写真を取得GToolStripMenuItem_Click(sender, e);
        }

        private void 終了XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 開くOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    long filesize = 0;
                    using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open))
                    {
                        filesize = fs.Length;
                        _imageFilePath = openFileDialog.FileName;
                        thumbnailPhoto.Image = Image.FromStream(fs);
                    }
                    txtInfo.Text = string.Format(INFOTEXT, thumbnailPhoto.Image.Size.ToString(), thumbnailPhoto.Image.PixelFormat.ToString(), filesize);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("例外出ちゃったよ\r\n" + ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 顔写真を取得GToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                thumbnailPhoto.Image = Properties.Resources.NoImage;
                txtInfo.Text = "";
                _imageFilePath = "";
                this.Refresh();

                byte[] ret = _adphoto.GetThumbnailPhoto();
                if (ret == null)
                {
                    thumbnailPhoto.Image = Properties.Resources.NoImage;
                    MessageBox.Show("顔写真は登録されていません。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    using (MemoryStream stmBLOBData = new MemoryStream(ret))
                    {
                        thumbnailPhoto.Image = Image.FromStream(stmBLOBData);
                    }
                    txtInfo.Text = string.Format(INFOTEXT, thumbnailPhoto.Image.Size.ToString(), thumbnailPhoto.Image.PixelFormat.ToString(), ret.Length);
                    this.Text = _adphoto.DisplayName + " の顔写真";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("例外出ちゃったよ\r\n" + ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 顔写真を更新UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (_imageFilePath == "")
                {
                    MessageBox.Show("ファイルメニューから顔写真を開いてください。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                byte[] bs;
                using (FileStream fs = new FileStream(_imageFilePath, FileMode.Open))
                {
                    bs = new byte[fs.Length];
                    if (bs.Length > 10000)
                    {
                        if( MessageBox.Show("ファイルサイズが10,000バイトをオーバーしています。続行しますか？",this.Text,MessageBoxButtons.YesNo,MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                        {
                            return;
                        }

                    }
                    fs.Read(bs, 0, bs.Length);
                    _adphoto.SetThumbnailPhoto(bs);
                    MessageBox.Show("顔写真を更新しました。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("例外出ちゃったよ\r\n" + ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 顔写真を消去DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("顔写真をActive Directoryから削除しますか？", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Cancel) return;
                _adphoto.ClearThumbnailPhoto();
                MessageBox.Show("顔写真をActive Directory上から削除しました。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                thumbnailPhoto.Image = Properties.Resources.NoImage;
                txtInfo.Text = "";
                _imageFilePath = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("例外出ちゃったよ\r\n" + ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
